#include <avr/io.h>
#include "i2cbar.h"
#include "irbar.h"



volatile uint8_t irBarValue[NUM_IRS+1];
uint8_t _irBar_enabled;
uint8_t _irBar_in_progress;


void irBarSetup(void) {

	// set up IR bar control pin to output
	PORT_OFF(CTRL_PORT, CTRL_IR_BAR);
	PORT_ON(CTRL_PORT_DDR, CTRL_IR_BAR);
	
	
	// set up IR bar LCRs to input
	PORT_ALL_OFF(INPUT_PORT, (1 << PIN_IR_0) | (1 << PIN_IR_1) | (1 << PIN_IR_2) | (1 << PIN_IR_3) | (1 << PIN_IR_4) );
	PORT_ALL_OFF(INPUT_PORT_DDR, (1 << PIN_IR_0) | (1 << PIN_IR_1) | (1 << PIN_IR_2) | (1 << PIN_IR_3) | (1 << PIN_IR_4) );
	PORT_ALL_OFF(INPUT_PORT_PIN, (1 << PIN_IR_0) | (1 << PIN_IR_1) | (1 << PIN_IR_2) | (1 << PIN_IR_3) | (1 << PIN_IR_4) );
	
	//_irBar_in_progress = 0;
	_irBar_enabled = 1;

}



void irBarDo(void) {
	// prepare
	
	// Switch IR Pins to OUPUT
	PORT_ALL_ON(INPUT_PORT_DDR, (1 << PIN_IR_0) | (1 << PIN_IR_1) | (1 << PIN_IR_2) | (1 << PIN_IR_3) | (1 << PIN_IR_4) );   
	// Bring Pins High for 10us to charge capacitor
	PORT_ALL_ON(INPUT_PORT, (1 << PIN_IR_0) | (1 << PIN_IR_1) | (1 << PIN_IR_2) | (1 << PIN_IR_3) | (1 << PIN_IR_4) );  
	delay_us(20); 


	// do

	PORT_ALL_OFF(INPUT_PORT, (1 << PIN_IR_0) | (1 << PIN_IR_1) | (1 << PIN_IR_2) | (1 << PIN_IR_3) | (1 << PIN_IR_4) );  
	// Switch IR Pins to INPUT
	PORT_ALL_OFF(INPUT_PORT_DDR, (1 << PIN_IR_0) | (1 << PIN_IR_1) | (1 << PIN_IR_2) | (1 << PIN_IR_3) | (1 << PIN_IR_4) );   
	// No Pull up
	PORT_ALL_OFF(INPUT_PORT, (1 << PIN_IR_0) | (1 << PIN_IR_1) | (1 << PIN_IR_2) | (1 << PIN_IR_3) | (1 << PIN_IR_4) );   
	// ?
	//PORT_ALL_ON(INPUT_PORT_PIN, (1 << PIN_IR_0) | (1 << PIN_IR_1) | (1 << PIN_IR_2) | (1 << PIN_IR_3) | (1 << PIN_IR_4) );   

	// delay_us(5); 
	_irBar_in_progress = 1;

}



uint8_t tmp_irBarValue[NUM_IRS];


void irBarLoop(void){

	static unsigned long _irBar_start_millis;
	static uint8_t _finished;
	static uint16_t delta;

	if (_irBar_enabled) {
		
		
		delta =  (actualMillis - _irBar_start_millis) ;
		
		if(!(_irBar_in_progress)) {
			
			if(delta > 20) {
			
				irBarLightOn();
				irBarDo();
				_irBar_start_millis = actualMillis; 
				_finished = 0;
				//led1Off();
				if(_buzzer_sound_nr == 6 && irBarValue[NUM_IRS] > 0x20) { // only beep alert
					BUZZER_PERIOD(255);//8
					BUZZER_PULSE(1);//6
					BUZZER_START();	
				}
				for(uint8_t i = 0; i < NUM_IRS; i++) 
					tmp_irBarValue[i] = 0;	
			}
		} 
		else {
			
			for(uint8_t i = 0; i < NUM_IRS; i++) {
				if((!tmp_irBarValue[i]) && ( PORT_IS_OFF(INPUT_PORT_PIN, (PIN_IR_0 + i)))) {
					tmp_irBarValue[i] = (uint8_t)(( actualMillis - _irBar_start_millis ) ) ;
					irBarValue[i] = tmp_irBarValue[i];
					_finished++;
				}
			}
			
			if (_finished == NUM_IRS) {
				if(_buzzer_sound_nr == 6) {
					BUZZER_STOP();		
				}
				
				irBarValue[NUM_IRS] = (uint8_t)(( actualMillis - _irBar_start_millis ) ) ; // max
				_irBar_in_progress = 0;
				irBarLightOff();
				_irBar_start_millis = actualMillis; 
				//led1On();
			}
			
			
			
			
		}
	}
	
}



